home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Pascal Super Library
/
Pascal Super Library (CW International)(1997).bin
/
LIBRARY
/
PAS_0793
/
SHRTBASE.PAS
< prev
next >
Wrap
Pascal/Delphi Source File
|
1993-08-01
|
2KB
|
49 lines
{─ Fido Pascal Conference ────────────────────────────────────────────── PASCAL ─
Msg : 290 of 314
From : Ron Poulton 1:153/9107.0 06 Jul 93 21:44
To : Mark Lewis
Subj : Easy Base Conversion
────────────────────────────────────────────────────────────────────────────────
Not to break your routine, but here's something people may find of use.
It converts a decimal numeric to a string in any other base:}
Function Dec2Base(Decm : LongInt;Base,Len : Byte) : String;
{ D is the decimal number, B is the Base to convert it to, and Lis
the length of the return string (if the value is shorter, it will be
leftmostly padded with "0"'s }
Var
SStr : String;
I : Byte;
H : LongInt;
Begin
SStr[0]:=Chr(Len);
For I:=1 to Len do Begin
H:=Decm;
Decm:=H Div Base;
Dec (H,Decm*Base);
SStr[Len-I+1]:=Chr(H+48+7*Ord(H>9)){+SStr};
End;
Dec2Base:=SStr;
End;
{DECM is the longinteger value of the number you want to convert, BASE is
the destination base to convert to, and LEN is the maximum length of the
resulting string. Its counterpart converts a string numeric in a certain
base to its decimal numeric value:}
Function Base2Dec(Num : String;Base : Byte) : LongInt;
Var
Ret : LongInt;
I : Byte;
H : LongInt;
Len : Byte Absolute Num;
Begin
Ret:=0;
For I:=1 to Len do Begin
H:=Ord(Num[I])-48;
Ret:=Ret*Base+H-7*Ord(H>9);
End;
Base2Dec:=Ret;
End;
NUM is the string to convert, BASE is the base it's in. Enjoy! :>